package util;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import model.PlagiarizeEntity;
import model.SimilarityOutEntity;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.util.List;


/*
 easyExcel 工具类(仅导出)中可自定义样式格式等
 */
public class EasyExcelUtil {


    /*
     导出 Excel ：一个 sheet，带表头.
     detailList,sortMaxResultList,SimilarityOutList  详细结果，简略结果，抄袭结果
     filepath    导出的文件名
     */
    public static void writeExcel(String filepath, List<SimilarityOutEntity> detailList, List<SimilarityOutEntity> sortMaxResultList,
                                  List<PlagiarizeEntity> plagiarizeEntityList) {
        //调用工具类,导出excel

        // 头的策略
        //单元格风格类
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();//new一个单元格对象，作为表头单元格对象
        //设置表头居中对齐 //设置水平对齐方式
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());//前景色
        WriteFont headWriteFont = new WriteFont();//new一个字体对象
        headWriteFont.setFontHeightInPoints((short) 10);//设置字体高度，参数为short类型的值，单位是px

        // 字体
        headWriteCellStyle.setWriteFont(headWriteFont);//将字体对象添加到单元格风格对象
        headWriteCellStyle.setWrapped(true);//自动换行

        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();//new一个单元格，作为内容单元格对象
        //设置内容靠中对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//水平居中

        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        //水平单元格风格策略，将表头和内容风格写成一个对象
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭

        // 链式调用
              ExcelWriter excelWriter = EasyExcel.write(filepath)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .build();
              //传入路径，文件类型，表风格，创建excel的输出流

        //详细结果
        WriteSheet sheetDetail = EasyExcel.writerSheet(0, "详细结果").head(SimilarityOutEntity.class).build();

        excelWriter.write(detailList, sheetDetail);

        //简略结果
        WriteSheet sheetMax = EasyExcel.writerSheet(1, "简略结果").head(SimilarityOutEntity.class).build();
        excelWriter.write(sortMaxResultList, sheetMax);

        //抄袭名单
        WriteSheet sheetPlagiarize = EasyExcel.writerSheet(2, "抄袭名单").head(PlagiarizeEntity.class).build();
        excelWriter.write(plagiarizeEntityList, sheetPlagiarize);

        excelWriter.finish();//关闭表格写入，释放资源













/*
        //简单语句创建
        ExcelWriterBuilder excelwriterbuilder=  EasyExcel.write(filepath, SimilarityOutEntity.class); //前为excelwriter传入实体类作为表头

        ExcelWriterBuilder excelwriterbuilder=  EasyExcel.write(filepath);
        excelwriterbuilder.excelType(ExcelTypeEnum.XLSX);//设定其表格类型
        excelwriterbuilder.registerWriteHandler(horizontalCellStyleStrategy);//设定其表格水平对齐方式，参数为回评对齐方式策略对象
        ExcelWriter excelWriter = excelwriterbuilder.build();
*/
        /*       WriteSheet sheetDetail = new WriteSheet();
        sheetDetail.setSheetName("详细结果");
        sheetDetail.setSheetNo(0);
        excelWriter.write(detailList, sheetDetail);*/
        //sheetDetail.setHead();//需要传递一个string集合，此处方法不可用，改法：使用链式调用创建，或者提前为excelwriter传入实体类作为表头




    }


}

